package com.open.capacity.points.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.SysUser;
import com.open.capacity.common.web.Result;
import com.open.capacity.log.annotation.LogAnnotation;
import com.open.capacity.points.feign.UserFeignClient;
import com.open.capacity.points.service.PointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("points")
@Api(tags = "POINTS API")
public class PointsController {

    @Resource
    private PointsService pointsService;
    @Resource
    private UserFeignClient userFeignClient;

    @GetMapping("/userId/{userId}")
    @ApiOperation(value = "根据userid，获取当前积分")
//    @PreAuthorize("hasAnyAuthority('menu:post/menus','menu:put/menus')")
    @LogAnnotation(module = "points-center")
    public Result<Integer> findPointsByUserId(@PathVariable long userId) throws ControllerException {
        int currentPoints;
        try {
            SysUser user = new SysUser();
            user.setId(userId);
            currentPoints = pointsService.findCurrentPoints(user);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
        return Result.succeed(currentPoints, "成功");
    }

    @GetMapping("/username/{username}")
    @ApiOperation(value = "根据username，获取当前积分")
    @LogAnnotation(module = "points-center")
    public Result<Integer> findPointsByUsername(@PathVariable String username) throws ControllerException {
        int currentPoints;
        try {
            SysUser user = new SysUser();
            user.setUsername(username);
            currentPoints = pointsService.findCurrentPoints(user);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
        return Result.succeed(currentPoints, "成功");
    }

    @GetMapping("/current")
    @ApiOperation(value = "查询当前登录用户积分")
    @LogAnnotation(module = "points-center")
    public Result<Integer> findPoints() throws ControllerException {
        int currentPoints;
        try {
            SysUser user = userFeignClient.getLoginAppUser();
            currentPoints = pointsService.findCurrentPoints(user);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
        return Result.succeed(currentPoints, "成功");
    }

    @GetMapping("/obtain")
    @ApiOperation(value = "查询累计获得积分")
    @LogAnnotation(module = "points-center")
    public Result obtain(Long userId) throws ControllerException {
        try {
            return pointsService.findObtainPoints(userId);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @GetMapping("/todayOfObtain")
    @ApiOperation(value = "查询今日获得积分")
    @LogAnnotation(module = "points-center")
    public Result todayOfObtain(Long userId) throws ControllerException {
        try {
            return pointsService.findTodayObtainPoints(userId);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @GetMapping("/consume")
    @ApiOperation(value = "查询累计消费积分")
    @LogAnnotation(module = "points-center")
    public Result consume(Long userId) throws ControllerException {
        try {
            return pointsService.findConsumePoints(userId);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @GetMapping("/todayOfConsume")
    @ApiOperation(value = "查询今日消费积分")
    @LogAnnotation(module = "points-center")
    public Result todayOfConsume(Long userId) throws ControllerException {
        try {
            return pointsService.findTodayConsume(userId);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * @param userId         产生积分变动的用户Id
     * @param pointsRuleCode 积分规则  PointsRuleConstant
     * @param points         用户设定的积分值 默认为0
     */
    @PostMapping("/change")
    @ApiOperation(value = "积分变动")
    @LogAnnotation(module = "points-center")
    public Result change(Long userId, String pointsRuleCode, int points) throws ControllerException {
        try {
            return pointsService.change(userId, pointsRuleCode, points);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }
}
